let username = document.querySelector("#username");
let tel = document.querySelector('#tel');
let password = document.querySelector("#password");
let form = document.querySelector("form");
let captcha = document.querySelector('#captcha');
let cpa = document.querySelector('#cpa');
let btn = document.querySelector('#btn');

let username1 = /(^\u4e00-\u9fa5]{2,7}|[a-z]{1,14}$)/i;
let tel1 = /^1[3-9]\d{9}$/;

cpa.onclick = function () {
    let e = window.event;
    e.preventDefault();
    let number = parseInt(Math.random() * (1000000 - 100000 + 1) + 100000);
    cpa.innerHTML = number;
}
form.onsubmit = function () {
    if (username1.test(username.value)) {
        alert('请输入正确的用户名')
        return
    }
    else if (!tel1.test(tel.value)) {
        alert('请输入正确的手机号')
        return
    }else if(!(cpa.innerHTML == captcha.value)){
        alert('请输入正确的验证码')
        return
    }
    let e = window.event;
    e.preventDefault();
    pAjax({
        type: 'post',
        url: '../api/reg.php',
        data: {
            username: username.value,
        }
    }).then(res => {
        res = JSON.parse(res);
        console.log(res);
        if (res.code == 0) {
            location.href="./login.html"
        }
    })
}